package com.my.blog.website.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my.blog.website.constant.WebConst;
import com.my.blog.website.dao.CarouselVoMapper;
import com.my.blog.website.exception.TipException;
import com.my.blog.website.modal.Vo.CarouselVo;
import com.my.blog.website.modal.Vo.CarouselVoExample;
import com.my.blog.website.service.ICarouselService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 郭以华 on 2019/3/4.
 */
@Service
public class CarouselServiceImpl implements ICarouselService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CarouselServiceImpl.class);

    @Resource
    private CarouselVoMapper carouselVoMapper;

    /**
     * 获取轮播分页
     *
     * @param page  当前页
     * @param limit 每页条数
     * @return 轮播
     */
    @Override
    public PageInfo<CarouselVo> getCarouselsWithpage(CarouselVoExample carouselVoExample, int page, int limit) {
        LOGGER.debug("Enter getCarousels method:page={},linit={}", page, limit);
        if (page <= 0) {
            page = 1;
        }
        if (limit < 1 || limit > WebConst.MAX_POSTS) {
            limit = 10;
        }
        PageHelper.startPage(page, limit);
        List<CarouselVo> carouselVos = carouselVoMapper.selectByExample(carouselVoExample);
        return new PageInfo<>(carouselVos);
    }

    /**
     * 获取启用数量
     *
     * @return
     */
    @Override
    public String getCarouselStatusY() {
        CarouselVoExample carouselVoExample = new CarouselVoExample();
        CarouselVoExample.Criteria criteria = carouselVoExample.createCriteria();
        criteria.andFlagEqualTo("y");
        int count = ((int) carouselVoMapper.countByExample(carouselVoExample));
        if (count >= 4) {
            return "over";
        } else {
            return "yes";
        }
    }

    /**
     * 更新轮播状态
     *
     * @param id
     * @param cflag
     */
    @Override
    public void updateCarouselStatus(int id, String cflag) {
        CarouselVo carouselVo = new CarouselVo();
        carouselVo.setFlag(cflag);
        CarouselVoExample carouselVoExample = new CarouselVoExample();
        carouselVoExample.createCriteria().andIdEqualTo(id);
        carouselVoMapper.updateByExampleSelective(carouselVo, carouselVoExample);
    }

    @Override
    public void deleteCarouselById(int cid) {
        carouselVoMapper.deleteByPrimaryKey(cid);
    }

    /**
     * 发布轮播
     *
     * @param carouselVo
     */
    @Override
    public void publish(CarouselVo carouselVo) {
        if (null == carouselVo) {
            throw new TipException("轮播对象为空");
        }
        if (StringUtils.isBlank(carouselVo.getTitle())) {
            throw new TipException("轮播标题不能为空");
        }
        if (StringUtils.isBlank(carouselVo.getUrl())) {
            throw new TipException("轮播图片没有上传");
        }
        carouselVoMapper.insert(carouselVo);
    }

    /**
     * 获取轮播列表
     *
     * @return
     */
    @Override
    public List<CarouselVo> getCarouselList() {
        CarouselVoExample carouselVoExample = new CarouselVoExample();
        carouselVoExample.setOrderByClause("rank desc");
        carouselVoExample.or().andFlagEqualTo("y");
        List<CarouselVo> carouselVoList = carouselVoMapper.selectByExample(carouselVoExample);
        return carouselVoList;
    }
}
